package com.xbongbong.pro.statistic.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.errorcodes.StatisticsServicesErrorCodeEnum;
import com.xbongbong.pro.statistic.pojo.DepartmentPkPojo;
import com.xbongbong.pro.statistic.pojo.dto.DepIncludeSubDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkAddDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkEditDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkListDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkSetSaveDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkSetSaveInfoDTO;
import com.xbongbong.pro.statistic.pojo.dto.DepartmentPkSubSaveDTO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkAddVO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkEditVO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkListVO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkSetSaveVO;
import com.xbongbong.pro.statistic.pojo.vo.DepartmentPkSubSaveVO;
import com.xbongbong.pro.statistic.service.DepartmentPkService;
import com.xbongbong.saas.constant.pk.DepartmentPkConstant;
import com.xbongbong.saas.domain.entity.DepartmentPkEntity;
import com.xbongbong.saas.model.DepartmentPkModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 部门pk方案相关
 *
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/3/21 10:58
 * @since v1.0
 */
@Service("departmentPkService")
public class DepartmentPkServiceImpl implements DepartmentPkService {
    @Resource
    private DepartmentPkModel departmentPkModel;

    /**
     * 获取部门绩效pk方案列表
     *
     * @param departmentPkListDTO 部门pk方案列表入参
     * @return com.xbongbong.pro.statistic.pojo.vo.DepartmentPkListVO
     * @author 徐俊杰
     * @date 2019/3/21 11:02
     * @since v1.0
     */
    @Override
    public DepartmentPkListVO departmentPkList(DepartmentPkListDTO departmentPkListDTO) throws XbbException {
        DepartmentPkListVO departmentPkListVO = new DepartmentPkListVO();
        List<DepartmentPkPojo> departmentPkPojoList = new ArrayList<>();
        String corpid = departmentPkListDTO.getCorpid();
        //获取部门pk方案实体列表
        List<DepartmentPkEntity> departmentPkEntityList = departmentPkModel.getDepartmentPkList(corpid);
        //遍历实体列表，向pojo中加入数据
        for (DepartmentPkEntity entity : departmentPkEntityList) {
            DepartmentPkPojo departmentPkPojo = new DepartmentPkPojo();
            BeanUtil.copyProperties(entity, departmentPkPojo);
            //把pojo放入list中
            departmentPkPojoList.add(departmentPkPojo);
        }
        departmentPkListVO.setList(departmentPkPojoList);
        return departmentPkListVO;
    }

    /**
     * 部门pk方案新建
     *
     * @param departmentPkAddDTO 部门pk方案新建入参
     * @return DepartmentPkAddVO
     * @throws XbbException 插入数据时抛出异常
     * @author 徐俊杰
     * @date 2019/3/22 20:10
     * @since v1.0
     */
    @Override
    public DepartmentPkAddVO departmentPkAdd(DepartmentPkAddDTO departmentPkAddDTO) throws XbbException {
        DepartmentPkAddVO departmentPkAddVO = new DepartmentPkAddVO();
        //校验方案数量
        String corpid = departmentPkAddDTO.getCorpid();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("del", DelEnum.NORMAL.getDel());
        int count = departmentPkModel.getEntitysCount(param);
        if (count >= DepartmentPkConstant.MAXIMUM_SCHEMES) {
            throw new XbbException(StatisticsServicesErrorCodeEnum.API_ERROR_224053);
        }
        //插入数据
        DepartmentPkEntity departmentPkEntity = new DepartmentPkEntity();
        BeanUtil.copyProperties(departmentPkAddDTO, departmentPkEntity);
        List<DepIncludeSubDTO> depIncludeSub = departmentPkAddDTO.getDepIncludeSub();
        //转为JSONArray
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(depIncludeSub);
        departmentPkEntity.setDepIncludeSub(jsonArray);
        departmentPkModel.insert(departmentPkEntity);
        //插入后entity更新
        Long id = departmentPkEntity.getId();
        departmentPkAddVO.setId(id);
        return departmentPkAddVO;
    }

    /**
     * 部门pk方案编辑
     *
     * @param departmentPkEditDTO 部门pk方案编辑入参
     * @return DepartmentPkEditVO
     * @throws XbbException 更新数据时抛出异常
     * @author 徐俊杰
     * @date 2019/3/22 20:14
     * @since v1.0
     */
    @Override
    public DepartmentPkEditVO departmentPkEdit(DepartmentPkEditDTO departmentPkEditDTO) throws XbbException {
        DepartmentPkEditVO departmentPkEditVO = new DepartmentPkEditVO();
        //查询要更新的方案
        Long id = departmentPkEditDTO.getId();
        String corpid = departmentPkEditDTO.getCorpid();
        DepartmentPkEntity departmentPkEntity = departmentPkModel.getByKey(id, corpid);
        if (departmentPkEntity == null) {
            throw new XbbException(StatisticsServicesErrorCodeEnum.API_ERROR_224024, StatisticsServicesErrorCodeEnum.API_ERROR_224024.getMsg(), I18nMessageUtil.getMessage(I18nStringConstant.DEPARTMENT_PK_PROGRAM));
        }
        //设置名称
        String name = departmentPkEditDTO.getName();
        departmentPkEntity.setName(name);
        //设置部门
        List<DepIncludeSubDTO> depIncludeSub = departmentPkEditDTO.getDepIncludeSub();
        //转为JSONArray
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(depIncludeSub);
        departmentPkEntity.setDepIncludeSub(jsonArray);
        departmentPkEntity.setDepIncludeSub(jsonArray);
        //更新
        departmentPkModel.update(departmentPkEntity);
        departmentPkEditVO.setId(departmentPkEntity.getId());
        return departmentPkEditVO;
    }

    /**
     * 部门pk方案-勾选是否包含下属部门
     *
     * @param departmentPkSubSaveDTO 部门pk方案-勾选是否包含下属部门入参
     * @return DepartmentPkSubSaveVO
     * @throws XbbException 更新数据时抛出异常
     * @author 徐俊杰
     * @date 2019/3/24 10:15
     * @since v1.0
     */
    @Override
    public DepartmentPkSubSaveVO departmentPkSubSave(DepartmentPkSubSaveDTO departmentPkSubSaveDTO) throws XbbException {
        DepartmentPkSubSaveVO departmentPkSubSaveVO = new DepartmentPkSubSaveVO();
        //查询要更新的方案
        Long id = departmentPkSubSaveDTO.getId();
        String corpid = departmentPkSubSaveDTO.getCorpid();
        DepartmentPkEntity departmentPkEntity = departmentPkModel.getByKey(id, corpid);
        if (departmentPkEntity == null) {
            throw new XbbException(StatisticsServicesErrorCodeEnum.API_ERROR_224024, StatisticsServicesErrorCodeEnum.API_ERROR_224024.getMsg(), I18nMessageUtil.getMessage(I18nStringConstant.DEPARTMENT_PK_PROGRAM));
        }
        List<DepIncludeSubDTO> depIncludeSub = departmentPkSubSaveDTO.getDepIncludeSub();
        //转为JSONArray
        JSONArray depIncludeSubArray = new JSONArray();
        depIncludeSubArray.addAll(depIncludeSub);
        int count = DepartmentPkConstant.DEFAULT_INCLUDE_NUM;
        //遍历部门，计算是否包含下属部门的部门个数
        for (int i = 0; i < depIncludeSubArray.size(); i++) {
            JSONObject dep = depIncludeSubArray.getJSONObject(i);
            if (dep.getBoolean("includeSub")) {
                count++;
            }
        }
        //判断是否包含下属部门的部门个数合法性
        if (count > DepartmentPkConstant.MAXIMUM_INCLUDE_DEPARTMENTS) {
            throw new XbbException(StatisticsServicesErrorCodeEnum.API_ERROR_224054);
        }
        //设置部门
        departmentPkEntity.setDepIncludeSub(depIncludeSubArray);
        //更新
        departmentPkModel.update(departmentPkEntity);
        departmentPkSubSaveVO.setId(departmentPkEntity.getId());
        return departmentPkSubSaveVO;
    }

    /**
     * 部门pk方案设置：删除、排序
     *
     * @param departmentPkSetSaveDTO 部门pk方案设置：删除、排序入参
     * @throws XbbException 更新数据时抛出异常
     * @return DepartmentPkSetSaveVO
     * @author 徐俊杰
     * @date 2019/3/24 11:12
     * @since v1.0
     */
    @Override
    public DepartmentPkSetSaveVO departmentPkSetSave(DepartmentPkSetSaveDTO departmentPkSetSaveDTO) throws XbbException {
        DepartmentPkSetSaveVO departmentPkSetSaveVO = new DepartmentPkSetSaveVO();
        //将入参处理成idSortMap和idSet
        Map<Long, Integer> idSortMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Set<Long> idSet = new HashSet<>();
        List<DepartmentPkSetSaveInfoDTO> infoDTOList = departmentPkSetSaveDTO.getList();
        for (DepartmentPkSetSaveInfoDTO dto : infoDTOList) {
            Long id = dto.getId();
            Integer sort = dto.getSort();
            idSortMap.put(id, sort);
            idSet.add(id);
        }
        //获取当前公司所有方案
        long now = DateUtil.getInt();
        String corpid = departmentPkSetSaveDTO.getCorpid();
        List<DepartmentPkEntity> departmentPkEntityList = departmentPkModel.getDepartmentPkList(corpid);
        if (departmentPkEntityList.size() == 0) {
            return departmentPkSetSaveVO;
        }
        for (DepartmentPkEntity entity : departmentPkEntityList) {
            Long id = entity.getId();
            if (idSet.contains(id)) {
                //如果方案id在传入id集合中，则重置sort
                entity.setSort(idSortMap.get(id));
            } else {
                //如果方案id不在传入id集合中，则删除
                entity.setDel(DelEnum.DELETE.getDel());
            }
            entity.setUpdateTime(now);
        }
        departmentPkModel.updateBatch(departmentPkEntityList, corpid);
        return departmentPkSetSaveVO;
    }
}
